package com.youxin.cpcenter.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.common.es.bean.RequestContactBean;
import com.youxin.busfoundation.common.es.bean.RequestQueryLeadsBean;
import com.youxin.busfoundation.utils.es.EsBasePage;
import com.youxin.cpcenter.common.log.LogMethod;
import com.youxin.cpcenter.service.SearchService;
import com.youxin.foundation.utils.ObjectUtils;

/**
 * 搜索公司和线索
 * @author yingde.cao
 *
 */
@RestController
public class SearchController extends BaseController{
	
	@Autowired
	private SearchService searchService;
	/**
	 * 高级搜索接口
	 * @param request
	 * @param httpServletResponse
	 * @param reqeustBean RequestQueryLeadsBean
	 * @return ResponseObject
	 */
	@LogMethod(id="ent.search",eventContent={})
	@RequestMapping(value = "/api/search", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject search(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestQueryLeadsBean reqeustBean) {
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			EsBasePage<Object> result=searchService.search(reqeustBean);
			return new ResponseObject(result);
		} catch (Exception e) {
			logger.error("/api/search异常", e);
			return ResponseObject.systemError(e);
		}
	}
	/**
	 * OA调用的搜索,目前没有使用
	 * @param request
	 * @param httpServletResponse
	 * @param reqeustBean
	 * @return
	 */
	@LogMethod(id="ent.oa.search",eventContent={})
	@RequestMapping(value = "/api/identify/search", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject identifySearch(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestQueryLeadsBean reqeustBean) {
		try {
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			EsBasePage<Object> result=searchService.identifySearch(reqeustBean);
			return new ResponseObject(result);
		} catch (Exception e) {
			logger.error("/api/identify/search异常", e);
			return ResponseObject.systemError(e);
		}
	}
	/**
	 * 联系人信息查询
	 * @param request
	 * @param httpServletResponse
	 * @param reqeustBean
	 * @return
	 */
	@LogMethod(id="ent.api.contact",eventContent={})
	@RequestMapping(value = "/api/contact", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject queryContact(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestContactBean reqeustBean)  {
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			ObjectUtils.requestNonEmpty(reqeustBean.getCompanyUid(), "企业标识不能为空");
			reqeustBean.setPageSize(100);
			EsBasePage<Object> result=searchService.searchContact(reqeustBean);
			return new ResponseObject(result);
		} catch (Exception e) {
			logger.error("/queryContact异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
}
